import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache, Store } from 'cache-manager';

/**
 * store
 * {
  name: 'redis',
  getClient: [Function: getClient],
  set: [Function: set],
  mset: [Function: mset],
  get: [Function: get],
  mget: [Function: mget],
  del: [Function: del],
  reset: [Function: reset],
  keys: [Function: keys],
  ttl: [Function: ttl],
  isCacheableValue: [Function]
}
 * **/


interface RedisStore extends Store {
    getClient? : {
        () : any
    }
}

@Injectable()
export class RedisService {
    constructor(@Inject(CACHE_MANAGER) private cacheManager : Cache) {}
    get<T>(key) : Promise<T> {
        return this.cacheManager.get<T>(key)
    }

    set<T>(key:string, value:T, ttl:number|null = null){
        this.cacheManager.set<T>(key, value, { ttl })
    }

    get client() {
        const store:RedisStore = this.cacheManager.store
        return store.getClient()
    }
}
